package com.spirng.leetcode.day06;

import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.Callable;


public class Test222 {
    public static int value=1;
    static {
        value=2;
    }

//    public static void main(String[] args) {
////        TreeNode root = new TreeNode(1,new TreeNode(2),new TreeNode(3));
////        Test222 test222 = new Test222();
////        test222.countNodes1(root);
//
////        System.out.println(String.class.getClassLoader());
//        File file = new File("C:\\Users\\l'h\\Desktop\\docker.txt");
//        BufferedReader reader = null;
//
//        try {
//            reader = new BufferedReader(new FileReader(file));
//            String line;
//            while ((line = reader.readLine()) != null) {
//                System.out.println(line);
//            }
//        } catch (IOException e) {
//            System.err.println("An error occurred while reading the file.");
//            e.printStackTrace();
//        } finally {
//            try {
//                if (reader != null) {
//                    reader.close();
//                }
//            } catch (IOException e) {
//                System.out.println("Error closing file.");
//            }
//        }
//    }
public static void main(String[] args) {
    // 文件路径
    String filePath = "C:\\Users\\l'h\\Desktop\\docker.txt";

    // 读取文件内容
    String originalContent = readFile(filePath);

    // 打印原始内容到控制台
    System.out.println("Original content:");
    System.out.println(originalContent);

    // 获取用户输入作为修改后的内容
    System.out.println("\nEnter the modified content:");
    Scanner scanner = new Scanner(System.in);
    String modifiedContent = scanner.nextLine();

    // 写入修改后的内容到文件
    writeFile(filePath, modifiedContent);

    System.out.println("File has been successfully modified and saved.");
}

    // 读取文件内容
    public static String readFile(String filePath) {
        StringBuilder contentBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                contentBuilder.append(line).append(System.lineSeparator());
            }
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage());
            return null;
        }
        return contentBuilder.toString();
    }

    // 写入文件内容
    public static void writeFile(String filePath, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(content);
        } catch (IOException e) {
            System.err.println("Error writing to file: " + e.getMessage());
        }
    }
    public int countNodes(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        int cut=0;
        if(root==null){
            return cut;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.remove();
                cut++;
                if(node.left!=null){
                    queue.add(node.left);
                }
                if(node.right!=null){
                    queue.add(node.right);
                }
            }
        }
        return cut;
    }
    public  int countNodes1(TreeNode root) {
        if(root==null){
            return 0;
        }
        int left=countNodes1(root.left);
        int right=countNodes1(root.right);
        int result=left+right+1;
        return  result;
    }

}
